<?php 
/*
* StiPHPcms, A web Content management System made with PHP/MySQL
* JordSti : jord52@gmail.com
* Version : 0.0.2
*
* Copyright (C) 2009  jord52@gmail.com
*
* This program is free software; you can redistribute it and/or
* modify it under the terms of the GNU General Public License
* as published by the Free Software Foundation; either version 2
* of the License, or (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program; if not, write to the Free Software
* Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA  02110-1301, USA.
*/

class AvatarUploader
{
	private $max_x;
	private $max_y;
	private $destination;
	
	
	public function __construct($max_x = 120,$max_y = 120)
	{
		$this->max_x = $max_x;
		$this->max_y = $max_y;
		$this->destination = "images/avatar/";
	}

	public function getDestination()
	{
		return $this->destination;
	}
	
	public function upload($fieldName,$dest_name)
	{
		$tmp_file = $_FILES[$fieldName]['tmp_name'];
		
		if(!is_uploaded_file($tmp_file))
		{
			exit("File isn't uploaded !");
		}
		
		$filename = $_FILES[$fieldName]['name'];
		
		$file_ext = substr($filename,strlen($filename)-3,3);
		$file_ext = strtolower($file_ext);
		
		if($file_ext == 'jpg')
		{
			$this->uploadJPG($fieldName,$dest_name);
		}
		else if($file_ext == 'png')
		{
			$this->uploadPNG($fieldName,$dest_name);
		}
		else if($file_ext == 'gif')
		{
			$this->uploadGIF($fieldName,$dest_name);
		}
	}
	
	public function uploadJPG($fieldName,$dest_name)
	{
		$tmp_file = $_FILES[$fieldName]['tmp_name'];
		$img = imagecreatefromjpeg($tmp_file);
		
		$x = imagesx($img);
		$y = imagesy($img);
		
		if($x == $y)
		{
			$new_x = $this->max_x;
			$new_y = $this->max_y;
		}
		else if($x < $y)
		{
			$new_x = (($this->max_y*$x)/$y);
			$new_y = $this->max_y;
		}
		else if($x > $y)
		{
			$new_y =  (($this->max_x*$y)/$x);
			$new_x = $this->max_x;
		}
		
		$new_img = imagecreatetruecolor($new_x,$new_y);
		
		imagecopyresized($new_img,$img,0,0,0,0,$new_x,$new_y,$x,$y);
		
		$this->destination .= $dest_name.'.jpg';
		
		imagejpeg($new_img,$this->destination);
		
	}
	
	public function uploadPNG($fieldName,$dest_name)
	{
		$tmp_file = $_FILES[$fieldName]['tmp_name'];
		$img = imagecreatefrompng($tmp_file);
		
		$x = imagesx($img);
		$y = imagesy($img);
		
		if($x > $this->max_x || $y > $this->max_y)
		{
		
			if($x == $y)
			{
				$new_x = $this->max_x;
				$new_y = $this->max_y;
			}
			else if($x < $y)
			{
				$new_x = (($this->max_y*$x)/$y);
				$new_y = $this->max_y;
			}
			else if($x > $y)
			{
				$new_y =  (($this->max_x*$y)/$x);
				$new_x = $this->max_x;
			}
		
			$new_img = imagecreatetruecolor($new_x,$new_y);
		
			imagecopyresized($new_img,$img,0,0,0,0,$new_x,$new_y,$x,$y);
		}
		else
		{
			$new_img = $img;
		}
		$this->destination .= $dest_name.'.png';
		
		imagepng($new_img,$this->destination);
		
	}
	
	public function uploadGIF($fieldName,$dest_name)
	{
		$tmp_file = $_FILES[$fieldName]['tmp_name'];
		$img = imagecreatefromgif($tmp_file);
		
		$x = imagesx($img);
		$y = imagesy($img);
		
		if($x == $y)
		{
			$new_x = $this->max_x;
			$new_y = $this->max_y;
		}
		else if($x < $y)
		{
			$new_x = (($this->max_y*$x)/$y);
			$new_y = $this->max_y;
		}
		else if($x > $y)
		{
			$new_y =  (($this->max_x*$y)/$x);
			$new_x = $this->max_x;
		}
		
		$new_img = imagecreatetruecolor($new_x,$new_y);
		
		imagecopyresized($new_img,$img,0,0,0,0,$new_x,$new_y,$x,$y);
		
		$this->destination .= $dest_name.'.gif';
		
		imagegif($new_img,$this->destination);
		
	}
}


?>